package com.htsc.mrp.utils;

import com.htsc.mrp.po.Generator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    //处理字符串
    public static String handleTargetProject(String targetProject){
        String str = targetProject.replace("\\", "/");
        return str.substring(0, str.indexOf("java") + "java".length());
    }

    public static String handleTargetResources(String targetResources){
        String str = targetResources.replace("\\", "/");
        return str.substring(0, str.indexOf("resources") + "resources".length());
    }

    public static String handleModelTargetPackage(String modelTargetPackage){
        String str = modelTargetPackage.replace("\\", ".");
        return str.substring(str.indexOf("java") + "java".length()+1);
    }

    public static String handleMapperTargetPackage(String mapperTargetPackage){
        String str = mapperTargetPackage.replace("\\", ".");
        return str.substring(str.indexOf("resources") + "resources".length()+1);
    }

    public static String handleDaoTargetPackage(String daoTargetPackage){
        String str = daoTargetPackage.replace("\\", ".");
        return str.substring(str.indexOf("java") + "java".length()+1);
    }

    public static String handleJdbcUrl(int dbIpPort){
        return "jdbc:mysql://localhost:"+dbIpPort+ "/mrp";
    }

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     * 下划线转驼峰
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString().substring(0,1).toUpperCase()+sb.substring(1);
    }

    /**
     *  版本号处理
     */
    public static void handleVersion(Generator generator,String version){
        if (version == null){
            generator.setVersion("1.0");
        }else if (Integer.parseInt(version.substring(version.indexOf(".") + 1))< 9){
            String firstNumber = version.substring(0,version.indexOf("."));
            String secondNumber = version.substring(version.indexOf(".") + 1);
            generator.setVersion(firstNumber+"."+(Integer.parseInt(secondNumber)+1));
        }else {
            String firstNumber = version.substring(0,version.indexOf("."));
            generator.setVersion(Integer.parseInt(firstNumber)+1+".0");
        }
    }
}
